package ca.llsutherland.nfl.facade;

import java.util.List;

import ca.llsutherland.nfl.configuration.PoolConfiguration;
import ca.llsutherland.nfl.dao.ConferenceDao;
import ca.llsutherland.nfl.dao.DivisionDao;
import ca.llsutherland.nfl.dao.RoleDao;
import ca.llsutherland.nfl.dao.TeamDao;
import ca.llsutherland.nfl.dao.WeekTypeDao;
import ca.llsutherland.nfl.domain.Conference;
import ca.llsutherland.nfl.domain.Division;
import ca.llsutherland.nfl.domain.Role;
import ca.llsutherland.nfl.domain.Team;
import ca.llsutherland.nfl.domain.WeekType;
import ca.llsutherland.nfl.exception.ValidationException;
import ca.llsutherland.nfl.helper.ConferenceHelper;
import ca.llsutherland.nfl.helper.DivisionHelper;
import ca.llsutherland.nfl.helper.RoleHelper;
import ca.llsutherland.nfl.helper.TeamHelper;
import ca.llsutherland.nfl.helper.WeekTypeHelper;
import ca.llsutherland.nfl.persistence.Transaction;
import ca.llsutherland.nfl.persistence.TransactionHandler;
import ca.llsutherland.nfl.util.Constants;


public class AutoloadFacade extends BaseFacade {

	protected TransactionHandler transactionHandler = TransactionHandler.getInstance();

	public void autoloadRoles() {
		
		Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
            	List<String> roleNames = PoolConfiguration.getInstance().getAllSortedRoles();
            	for (String roleName : roleNames) {
            		Role role = RoleDao.getInstance().findByName(roleName);
            		if(role == null) {
            			Role newRole = RoleHelper.createNewRole(roleName);
            			RoleDao.getInstance().create(newRole);
            		}
				}
                return null;
            }
        };
        this.transactionHandler.runTransaction(t);
	}

	public void autoloadSeasonStructure() {
		Transaction t = new Transaction() {
            public Object executeTransaction() throws ValidationException {
            	
            	WeekType preSeason = WeekTypeDao.getInstance().findByName(Constants.WEEK_TYPE_PRE_SEASON);
            	if(preSeason == null){
            		preSeason = WeekTypeHelper.createNewWeekType(Constants.WEEK_TYPE_PRE_SEASON);
            		preSeason = WeekTypeDao.getInstance().create(preSeason);
            	}
            	
            	WeekType regularSeason = WeekTypeDao.getInstance().findByName(Constants.WEEK_TYPE_REGULAR_SEASON);
            	if(regularSeason == null){
            		regularSeason = WeekTypeHelper.createNewWeekType(Constants.WEEK_TYPE_REGULAR_SEASON);
            		regularSeason = WeekTypeDao.getInstance().create(regularSeason);
            	}
            	
            	WeekType postSeason = WeekTypeDao.getInstance().findByName(Constants.WEEK_TYPE_POST_SEASON);
            	if(postSeason == null){
            		postSeason = WeekTypeHelper.createNewWeekType(Constants.WEEK_TYPE_POST_SEASON);
            		postSeason = WeekTypeDao.getInstance().create(postSeason);
            	}
            	
            	
                return null;
            }
        };
        this.transactionHandler.runTransaction(t);
	}
	
	public void autoloadLeagueStructure() {
		Transaction t = new Transaction() {
			public Object executeTransaction() throws ValidationException {

				Conference afc = ConferenceDao.getInstance().findByName(Constants.CONFERENCE_NAME_AFC);
				if(afc == null){
					afc = ConferenceHelper.createNewConference(Constants.CONFERENCE_NAME_AFC);
					afc = ConferenceDao.getInstance().create(afc);
				}
				
				Conference nfc = ConferenceDao.getInstance().findByName(Constants.CONFERENCE_NAME_NFC);
				if(nfc == null){
					nfc = ConferenceHelper.createNewConference(Constants.CONFERENCE_NAME_NFC);
					nfc = ConferenceDao.getInstance().create(nfc);
				}
				
				Division afcEast = DivisionDao.getInstance().findByNameAndConference(Constants.DIVISION_EAST, afc);
				if(afcEast == null){
					afcEast = DivisionHelper.createNewDivision(Constants.DIVISION_EAST, afc);
					afcEast = DivisionDao.getInstance().create(afcEast);
				}
				
				Division afcNorth = DivisionDao.getInstance().findByNameAndConference(Constants.DIVISION_NORTH, afc);
				if(afcNorth == null){
					afcNorth = DivisionHelper.createNewDivision(Constants.DIVISION_NORTH, afc);
					afcNorth =DivisionDao.getInstance().create(afcNorth);
				}
				
				Division afcSouth = DivisionDao.getInstance().findByNameAndConference(Constants.DIVISION_SOUTH, afc);
				if(afcSouth == null){
					afcSouth = DivisionHelper.createNewDivision(Constants.DIVISION_SOUTH, afc);
					afcSouth = DivisionDao.getInstance().create(afcSouth);
				}
				
				Division afcWest = DivisionDao.getInstance().findByNameAndConference(Constants.DIVISION_WEST, afc);
				if(afcWest == null){
					afcWest = DivisionHelper.createNewDivision(Constants.DIVISION_WEST, afc);
					afcWest = DivisionDao.getInstance().create(afcWest);
				}

				Division nfcEast = DivisionDao.getInstance().findByNameAndConference(Constants.DIVISION_EAST, nfc);
				if(nfcEast == null){
					nfcEast = DivisionHelper.createNewDivision(Constants.DIVISION_EAST, nfc);
					nfcEast = DivisionDao.getInstance().create(nfcEast);
				}
				
				Division nfcNorth = DivisionDao.getInstance().findByNameAndConference(Constants.DIVISION_NORTH, nfc);
				if(nfcNorth == null){
					nfcNorth = DivisionHelper.createNewDivision(Constants.DIVISION_NORTH, nfc);
					nfcNorth = DivisionDao.getInstance().create(nfcNorth);
				}
				
				Division nfcSouth = DivisionDao.getInstance().findByNameAndConference(Constants.DIVISION_SOUTH, nfc);
				if(nfcSouth == null){
					nfcSouth = DivisionHelper.createNewDivision(Constants.DIVISION_SOUTH, nfc);
					nfcSouth =DivisionDao.getInstance().create(nfcSouth);
				}
				
				Division nfcWest = DivisionDao.getInstance().findByNameAndConference(Constants.DIVISION_WEST, nfc);
				if(nfcWest == null){
					nfcWest = DivisionHelper.createNewDivision(Constants.DIVISION_WEST, nfc);
					nfcWest = DivisionDao.getInstance().create(nfcWest);
				}
				
				//AUTOLOAD TEAMS NEXT
				createAfcEast(afcEast);
				createAfcNorth(afcNorth);
				createAfcWest(afcWest);
				createAfcSouth(afcSouth);

				createNfcEast(nfcEast);
				createNfcNorth(nfcNorth);
				createNfcSouth(nfcSouth);
				createNfcWest(nfcSouth);
				
				return null;
			}

			private void createNfcWest(Division nfcSouth) {
				Team sf = TeamDao.getInstance().findByCity(Constants.TEAM_SAN_FRANCISCO);
				if(sf == null){
					sf = TeamHelper.createNewTeam(Constants.TEAM_SAN_FRANCISCO, nfcSouth);
					TeamDao.getInstance().create(sf);
				}
				
				Team seattle = TeamDao.getInstance().findByCity(Constants.TEAM_SEATTLE);
				if(seattle == null){
					seattle = TeamHelper.createNewTeam(Constants.TEAM_SEATTLE, nfcSouth);
					TeamDao.getInstance().create(seattle);
				}
				
				Team arizona = TeamDao.getInstance().findByCity(Constants.TEAM_ARIZONA);
				if(arizona == null){
					arizona = TeamHelper.createNewTeam(Constants.TEAM_ARIZONA, nfcSouth);
					TeamDao.getInstance().create(arizona);
				}
				
				Team stLouis = TeamDao.getInstance().findByCity(Constants.TEAM_ST_LOUIS);
				if(stLouis == null){
					stLouis = TeamHelper.createNewTeam(Constants.TEAM_ST_LOUIS, nfcSouth);
					TeamDao.getInstance().create(stLouis);
				}
			}

			private void createNfcSouth(Division nfcSouth) {
				Team no = TeamDao.getInstance().findByCity(Constants.TEAM_NEW_ORLEANS);
				if(no == null){
					no = TeamHelper.createNewTeam(Constants.TEAM_NEW_ORLEANS, nfcSouth);
					TeamDao.getInstance().create(no);
				}
				
				Team atlanta = TeamDao.getInstance().findByCity(Constants.TEAM_ATLANTA);
				if(atlanta == null){
					atlanta = TeamHelper.createNewTeam(Constants.TEAM_ATLANTA, nfcSouth);
					TeamDao.getInstance().create(atlanta);
				}
				
				Team carolina = TeamDao.getInstance().findByCity(Constants.TEAM_CAROLINA);
				if(carolina == null){
					carolina = TeamHelper.createNewTeam(Constants.TEAM_CAROLINA, nfcSouth);
					TeamDao.getInstance().create(carolina);
				}
				
				Team tb = TeamDao.getInstance().findByCity(Constants.TEAM_TAMPA_BAY);
				if(tb == null){
					tb = TeamHelper.createNewTeam(Constants.TEAM_TAMPA_BAY, nfcSouth);
					TeamDao.getInstance().create(tb);
				}
			}

			private void createNfcNorth(Division nfcNorth) {
				Team gb = TeamDao.getInstance().findByCity(Constants.TEAM_GREEN_BAY);
				if(gb == null){
					gb = TeamHelper.createNewTeam(Constants.TEAM_GREEN_BAY, nfcNorth);
					TeamDao.getInstance().create(gb);
				}
				
				Team detroit = TeamDao.getInstance().findByCity(Constants.TEAM_DETROIT);
				if(detroit == null){
					detroit = TeamHelper.createNewTeam(Constants.TEAM_DETROIT, nfcNorth);
					TeamDao.getInstance().create(detroit);
				}
				
				Team chicago = TeamDao.getInstance().findByCity(Constants.TEAM_CHICAGO);
				if(chicago == null){
					chicago = TeamHelper.createNewTeam(Constants.TEAM_CHICAGO, nfcNorth);
					TeamDao.getInstance().create(chicago);
				}
				
				Team minnie = TeamDao.getInstance().findByCity(Constants.TEAM_MINNESOTA);
				if(minnie == null){
					minnie = TeamHelper.createNewTeam(Constants.TEAM_MINNESOTA, nfcNorth);
					TeamDao.getInstance().create(minnie);
				}
			}

			private void createNfcEast(Division nfcEast) {
				Team dallas = TeamDao.getInstance().findByCity(Constants.TEAM_DALLAS);
				if(dallas == null){
					dallas = TeamHelper.createNewTeam(Constants.TEAM_DALLAS, nfcEast);
					TeamDao.getInstance().create(dallas);
				}
				
				Team nyGiants = TeamDao.getInstance().findByCity(Constants.TEAM_NY_GIANTS);
				if(nyGiants == null){
					nyGiants = TeamHelper.createNewTeam(Constants.TEAM_NY_GIANTS, nfcEast);
					TeamDao.getInstance().create(nyGiants);
				}
				
				Team phillie = TeamDao.getInstance().findByCity(Constants.TEAM_PHILADELPHIA);
				if(phillie == null){
					phillie = TeamHelper.createNewTeam(Constants.TEAM_PHILADELPHIA, nfcEast);
					TeamDao.getInstance().create(phillie);
				}
				
				Team wash = TeamDao.getInstance().findByCity(Constants.TEAM_WASHINGTON);
				if(wash == null){
					wash = TeamHelper.createNewTeam(Constants.TEAM_WASHINGTON, nfcEast);
					TeamDao.getInstance().create(wash);
				}
			}

			private void createAfcSouth(Division afcSouth) {
				Team houston = TeamDao.getInstance().findByCity(Constants.TEAM_HOUSTON);
				if(houston == null){
					houston = TeamHelper.createNewTeam(Constants.TEAM_HOUSTON, afcSouth);
					TeamDao.getInstance().create(houston);
				}
				
				Team tenn = TeamDao.getInstance().findByCity(Constants.TEAM_TENNESSEE);
				if(tenn == null){
					tenn = TeamHelper.createNewTeam(Constants.TEAM_TENNESSEE, afcSouth);
					TeamDao.getInstance().create(tenn);
				}
				
				Team jax = TeamDao.getInstance().findByCity(Constants.TEAM_JACKSONVILLE);
				if(jax == null){
					jax = TeamHelper.createNewTeam(Constants.TEAM_JACKSONVILLE, afcSouth);
					TeamDao.getInstance().create(jax);
				}
				
				Team indie = TeamDao.getInstance().findByCity(Constants.TEAM_INDIANAPOLIS);
				if(indie == null){
					indie = TeamHelper.createNewTeam(Constants.TEAM_INDIANAPOLIS, afcSouth);
					TeamDao.getInstance().create(indie);
				}
			}

			private void createAfcWest(Division afcWest) {
				Team denver = TeamDao.getInstance().findByCity(Constants.TEAM_DENVER);
				if(denver == null){
					denver = TeamHelper.createNewTeam(Constants.TEAM_DENVER, afcWest);
					TeamDao.getInstance().create(denver);
				}
				
				Team oakland = TeamDao.getInstance().findByCity(Constants.TEAM_OAKLAND);
				if(oakland == null){
					oakland = TeamHelper.createNewTeam(Constants.TEAM_OAKLAND, afcWest);
					TeamDao.getInstance().create(oakland);
				}
				
				Team sd = TeamDao.getInstance().findByCity(Constants.TEAM_SAN_DIEGO);
				if(sd == null){
					sd = TeamHelper.createNewTeam(Constants.TEAM_SAN_DIEGO, afcWest);
					TeamDao.getInstance().create(sd);
				}
				
				Team kc = TeamDao.getInstance().findByCity(Constants.TEAM_KANSAS_CITY);
				if(kc == null){
					kc = TeamHelper.createNewTeam(Constants.TEAM_KANSAS_CITY, afcWest);
					TeamDao.getInstance().create(kc);
				}
			}

			private void createAfcNorth(Division afcNorth) {
				Team pitt = TeamDao.getInstance().findByCity(Constants.TEAM_PITTSBURGH);
				if(pitt == null){
					pitt = TeamHelper.createNewTeam(Constants.TEAM_PITTSBURGH, afcNorth);
					TeamDao.getInstance().create(pitt);
				}
				
				Team baltimore = TeamDao.getInstance().findByCity(Constants.TEAM_BALTIMORE);
				if(baltimore == null){
					baltimore = TeamHelper.createNewTeam(Constants.TEAM_BALTIMORE, afcNorth);
					TeamDao.getInstance().create(baltimore);
				}
				
				Team cinci = TeamDao.getInstance().findByCity(Constants.TEAM_CINCINNATI);
				if(cinci == null){
					cinci = TeamHelper.createNewTeam(Constants.TEAM_CINCINNATI, afcNorth);
					TeamDao.getInstance().create(cinci);
				}
				
				Team cleveland = TeamDao.getInstance().findByCity(Constants.TEAM_CLEVELAND);
				if(cleveland == null){
					cleveland = TeamHelper.createNewTeam(Constants.TEAM_CLEVELAND, afcNorth);
					TeamDao.getInstance().create(cleveland);
				}
			}

			private void createAfcEast(Division afcEast) {
				Team newEngland = TeamDao.getInstance().findByCity(Constants.TEAM_NEW_ENGLAND);
				if(newEngland == null){
					newEngland = TeamHelper.createNewTeam(Constants.TEAM_NEW_ENGLAND, afcEast);
					TeamDao.getInstance().create(newEngland);
				}
				
				Team nyJets = TeamDao.getInstance().findByCity(Constants.TEAM_NY_JETS);
				if(nyJets == null){
					nyJets = TeamHelper.createNewTeam(Constants.TEAM_NY_JETS, afcEast);
					TeamDao.getInstance().create(nyJets);
				}
				
				Team miami = TeamDao.getInstance().findByCity(Constants.TEAM_MIAMI);
				if(miami == null){
					miami = TeamHelper.createNewTeam(Constants.TEAM_MIAMI, afcEast);
					TeamDao.getInstance().create(miami);
				}
				
				Team buffalo = TeamDao.getInstance().findByCity(Constants.TEAM_BUFFALO);
				if(buffalo == null){
					buffalo = TeamHelper.createNewTeam(Constants.TEAM_BUFFALO, afcEast);
					TeamDao.getInstance().create(buffalo);
				}
			}
		};
		this.transactionHandler.runTransaction(t);
		
		
	}

	
	
	
}
